package com.eastseeway.radioproject.views;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.eastseeway.radioproject.R;

public class DigitalUnlockView extends ViewGroup implements View.OnClickListener{
    private int circleRadius = 50;

    private int textColor;

    private int horizontalInterval = 70;

    private int verticalInterval = 0;

    private int deleteTextSize = 16;

    private String[] data = new String[] {"1","2","3","4","5","6","7","8","9","","0","","","","DEL"};

    private String delText = CANCEL;

    public static final String DEL = "删除";
    public static final String CANCEL = "取消";
    public DigitalUnlockView(Context context) {
        super(context);
        init(context);
    }

    public DigitalUnlockView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DigitalUnlockView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        circleRadius = dp2px(context,20);
        horizontalInterval = dp2px(context,30);
        verticalInterval = dp2px(context,15);
        for (int i = 0; i< data.length; i++) {
            TextView textView = (TextView) LayoutInflater.from(context).inflate(R.layout.view_circle_text,null);

            textView.setWidth(circleRadius * 2);
            textView.setHeight(circleRadius * 2);

            textView.setText(data[i]);
            if (data[i].equals("DEL")) {
                textView.setText(delText);
            }
            if ("".equals(data[i])) {
                textView.setClickable(false);
            } else {
                textView.setOnClickListener(this);
            }

            if (i == data.length - 1) {
                textView.setTextSize(deleteTextSize);
            }
            addView(textView);
        }
    }

    public void setDelText(String text) {
        TextView textView = (TextView) getChildAt(getChildCount() - 1);
        textView.setText(text);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

        if (modeWidth == MeasureSpec.AT_MOST) {
            sizeWidth = circleRadius * 6 + horizontalInterval * 2 + getPaddingLeft() + getPaddingRight();
        }

        if (modeHeight == MeasureSpec.AT_MOST) {
            sizeHeight = circleRadius * 10 + 4 * verticalInterval + getPaddingTop() + getPaddingBottom();
        }
        setMeasuredDimension(sizeWidth,sizeHeight);
        for (int i = 0; i < getChildCount() ; i++) {
            getChildAt(i).measure(circleRadius * 2,circleRadius * 2);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int row = 0;
        int col = 0;
        for (int i = 0; i < getChildCount() ; i++) {
            row = i / 3;
            col = i % 3;
            int left = getPaddingLeft() + col * 2 * circleRadius + horizontalInterval * col;
            int top = getPaddingTop() + row * 2 * circleRadius + verticalInterval * row;
            int right = left + 2 * circleRadius;
            int bottom = top + 2 * circleRadius;
            getChildAt(i).layout(left,top,right,bottom);
        }
    }

    private int dp2px(Context context ,float dp) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        float density = displayMetrics.density;
        return (int)(density * dp + 0.5);
    }

    @Override
    public void onClick(View v) {
        TextView textView = (TextView) v;
        String num = textView.getText().toString();
        if (onNumberPressListener != null) {
            onNumberPressListener.onPress(num);
        }
    }

    public interface OnNumberPressListener {
        void onPress(String number);
    }
    private OnNumberPressListener onNumberPressListener;
    public void setOnNumberPressListener(OnNumberPressListener listener) {
        onNumberPressListener = listener;
    }
}
